import { getNavListAPI } from '@/api/home.js'
import { category } from '@/utils/constant.js'
export default {
  namespaced: true,
  state() {
    return {
      list: category.map((item, index) => ({
        id: index,
        name: item
      }))
    }
  },
  mutations: {
    getNavinfo(state, val) {
      state.list = val
    },
    updateStatus(state, payload) {
      // 修改分类的状态
      const cate = state.list.find(item => item.id === payload.id)
      if (cate) {
        cate.open = payload.open
      }
    }
  },
  actions: {
    async getNavList(store) {
      const res = await getNavListAPI()
      res.result.forEach(item => {
        item.open = false
      })
      if (res && res.result) {
        store.commit('getNavinfo', res.result)
      }
      console.log(res)
    }
  },
  getters: {
    cateList(state) {
      return state.list.map(item => {
        return {
          ...item,
          children:
            item.children &&
            item.children.filter((item, i) => {
              return i < 2
            })
        }
      })
    }
  }
}
